package com.zhajh.utils;

import java.io.IOException;
import java.io.InputStream;

import com.zhajh.R;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;

public class PlayerResFactory
{
    public static Bitmap bmpNameBg = null;
	public static Bitmap bmpDefaultHead = null;
	public static Bitmap bmpMoneyBg = null;
	public static Bitmap bmpOK = null;
	public static Bitmap bmpStatusBgRight = null;
	public static Bitmap bmpStatusBgLeft = null;
	//public static Bitmap bmpStatusBgBottom = null;

	private static int sBaseWidth = 0;
	private static int sBaseHeight = 0;
	private static int sNameViewHeight = 0;
	private static int sMoneyViewHeight = 0;

	public static void init(int baseW, int baseH, int nameViewH, int moneyViewH)
	{
		sBaseWidth = baseW;
		sBaseHeight = baseH;
		sNameViewHeight = nameViewH;
		sMoneyViewHeight = moneyViewH;
	}

    public static void ensureInitAllRes(Context context)
    {
    	int w = sBaseWidth;
    	int h = sBaseHeight;
        int headHeight = h - sNameViewHeight - sMoneyViewHeight;
        bmpNameBg = ensureInitBitmap(bmpNameBg, context, w, sNameViewHeight, R.drawable.infobg);
        bmpDefaultHead = ensureInitBitmap(bmpDefaultHead, context, w, headHeight, R.drawable.game_pphoto);
        bmpMoneyBg = ensureInitBitmap(bmpMoneyBg, context, w, sMoneyViewHeight, R.drawable.infobg);

        if (bmpOK == null)
        {
            bmpOK = initBitmap(context, "assets-hi/game_zhunbei.png");
        }

        if (bmpStatusBgRight == null)
        {
            bmpStatusBgRight = initBitmap(context, "assets-hi/game_tishikuang.png");
            //bmpStatusBgLeft = overturn(bmpStatusBgRight);
            //bmpStatusBgBottom = rotate(bmpStatusBgRight, 180);
            bmpStatusBgLeft = bmpStatusBgRight;
        }
    }

    public static Bitmap ensureInitBitmap(Bitmap bitmap, Context context, int w, int h, int resId)
    {
        if ((bitmap==null) || (bitmap.getWidth()!=w || bitmap.getHeight()!=h))
        {
        	if (bitmap != null)
        	{
        		bitmap.recycle();
        	}

        	bitmap = initBitmap(context, w, h, resId);
        }

        return bitmap;
    }

    public static Bitmap initBitmap(Context context, int w, int h, int resId)
	{
		Resources r = context.getResources();
        InputStream is = r.openRawResource(resId);
        BitmapDrawable  bmpDraw = new BitmapDrawable(is);
        return Bitmap.createScaledBitmap(bmpDraw.getBitmap(), w, h, false);
	}

    public static Bitmap initBitmap(Context context, int resId)
	{
		Resources r = context.getResources();
        InputStream is = r.openRawResource(resId);
        BitmapDrawable  bmpDraw = new BitmapDrawable(is);
        return bmpDraw.getBitmap();
	}

	public static Bitmap initBitmap(Context context, String assertsName)
	{
		try {
			InputStream is = context.getAssets().open(assertsName);
			BitmapDrawable  bmpDraw = new BitmapDrawable(is);
	        return bmpDraw.getBitmap();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}
	
	public static Bitmap createBitmapForWatermark(Bitmap src, Bitmap watermark) {
        if (src == null) {
            return null;
        }
        
        int w = src.getWidth();
        int h = src.getHeight();
        int ww = watermark.getWidth();
        int wh = watermark.getHeight();
        Bitmap newb = Bitmap.createBitmap(ww, wh, Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图
        Canvas cv = new Canvas(newb);
        
        Matrix m = new Matrix();
        m.postScale(1, -1);//上下翻转
        m.postRotate(-90);//左右翻转
        Bitmap newBitmap = Bitmap.createBitmap(src, 0, 0, w, h, m, true);
        cv.drawBitmap(newBitmap, new Rect(0, 0, newBitmap.getWidth(), newBitmap.getHeight()),new Rect(0, 0, ww, wh), null);
        
        cv.drawBitmap(watermark,0,0, null);// 前景图或水印
        
        cv.save(Canvas.ALL_SAVE_FLAG);// 保存
        // store
        cv.restore();// 存储
        return newb;
    }

	// 据说 rotate1 的速度要优于 rotate2
	public static Bitmap rotate1(Bitmap bitmap, final int degrees) {
		Matrix m = new Matrix();
		m.setRotate(degrees, (float) bitmap.getWidth() / 2, (float) bitmap.getHeight() / 2);
		//m.postScale(-1, 1);
		
		float targetX=0, targetY=0;

		if (degrees == 90) {
			targetX = bitmap.getHeight();
			targetY = 0;
		} else {
			targetX = bitmap.getHeight();
			targetY = bitmap.getWidth();
		}

		final float[] values = new float[9];
		m.getValues(values);

		float x1 = values[Matrix.MTRANS_X];
		float y1 = values[Matrix.MTRANS_Y];

		m.postTranslate(targetX - x1, targetY - y1);

		Bitmap bm1 = Bitmap.createBitmap(bitmap.getHeight(), bitmap.getWidth(), Bitmap.Config.ARGB_8888);

		Canvas canvas = new Canvas(bm1);
		canvas.drawBitmap(bitmap, m, null);

		return bm1;
	}
	
	public static Bitmap rotate2(Bitmap bitmap, int degrees) {
		if (bitmap == null)
			return null;

		Bitmap b2 = null;

        if (degrees != 0) {
            Matrix m = new Matrix();
            m.setRotate(degrees,
                    (float) bitmap.getWidth() / 2, (float) bitmap.getHeight() / 2);
            try {
                b2 = Bitmap.createBitmap(
                        bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);
                //if (bitmap != b2) {
                //    bitmap.recycle();
                //    bitmap = b2;
                //}
            } catch (OutOfMemoryError ex) {
            }
        }

        if (b2 == null)
        {
        	b2 = Bitmap.createBitmap(bitmap);
        }

        return b2;
    }
}
